package com.granada.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class MyWebAppConfiguration extends WebMvcConfigurationSupport {
    //静态虚拟访问路径
    @Value("${file.staticAccessPath}")
    private String staticAccessPath;

    @Value("${file.uploadFolderWin}")
    private String pathWin;

    @Value("${file.uploadFolderLinux}")
    private String pathLinux;

    @Value("${file.uploadFolderMac}")
    private String pathMac;

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

    /**
     * @Description:
     * 对文件的路径进行配置, 创建一个虚拟路径/imgs/**
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //判断是什么系统
        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")) {
            registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+pathWin);
        }else if(os.toLowerCase().startsWith("mac")){
            registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+pathMac);
        }else {
            registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+pathLinux);
        }

//        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+path);

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }

        super.addResourceHandlers(registry);
    }
}
